package com.cheyun.netsalev3.viewmodel

import android.util.Log
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.ObservableField
import androidx.lifecycle.MutableLiveData
import com.cheyun.netsalev3.bean.AnalysisParam
import com.cheyun.netsalev3.bean.AnalysisToPerParam
import com.cheyun.netsalev3.bean.DialogParam
import com.cheyun.netsalev3.repository.HomeAnalysisFragmentRepository
import com.cheyun.netsalev3.widget.MyDialog
import org.json.JSONObject
import java.lang.Exception


/**
 * @author Michael
 * @date  2018/12/19 - 11:24.
 */
class HomeAnalysisFragmentViewModel(var homeAnalysisFragmentRepository: HomeAnalysisFragmentRepository) : BaseViewModel() {
    var data = ObservableField<AnalysisToPerParam>()

    //业务类型  hall=> 展厅  ectel=> 网电销 secondary => 二网
    //时间类型  day 按天展开  month  按月展开
    var infotypename = MutableLiveData<String>()
    var datetypename = ObservableField<String>("本月")
    init {
        infotypename.postValue("网电销")
    }

    //展厅——本日月
    var hallMonth = arrayListOf(
            AnalysisParam("首次到店量", "1031", "（人）"),
            AnalysisParam("再次到店量", "1032", "（人）"),
            AnalysisParam("留档量", "1040", "（人）"),
            AnalysisParam("订车量", "1060", "（台）"),
            AnalysisParam("提车量", "1070", "（人）")
    )
    //展厅——本日月头部
    var hallMonthTop = arrayListOf(
            AnalysisParam("留档率", "1040-1031")

    )

    //网电销——本日月
    var ectelMonth = arrayListOf(
            AnalysisParam("线索量", "1010", "（条）"),
            AnalysisParam("有效线索量", "1020", "（人）"),
            AnalysisParam("首次到店量", "1031", "（台）"),
            AnalysisParam("再次进店量", "1032", "（人）"),
            AnalysisParam("订车量", "1060", "（台）"),
            AnalysisParam("提车量", "1070", "（台）")



    )
    //网电销——本日月---头部
    var ectelMonthTop = arrayListOf(
            AnalysisParam("首次到店率", "1031-1020"),
            AnalysisParam("再次进店率", "1032-1031"),
            AnalysisParam("当月有效线索率", "1020-1010")

    )

    //二网——本日月
    var secondaryMonth = arrayListOf(
//            AnalysisParam("留档量", "1040", "（人）"),
            AnalysisParam("订车量", "1060", "（台）"),
            AnalysisParam("提车量", "1070", "（人）")

    )
    //显示内容数据
    var showlistData = MutableLiveData<List<AnalysisParam>>()
    //显示头部数据
    var showTopData = ObservableField<List<AnalysisParam>>()

    //时间类型  day 按天展开  month  按月展开
    private var dtype: String = ""
    //业务类型  hall=> 展厅  ectel=> 网电销 secondary => 二网
    private var itype: String = ""

    private var infotype = arrayOf(
            DialogParam("展厅", "hall"),
            DialogParam("网电销", "ectel", "",true),
            DialogParam("二网", "secondary")
    )

    private var datetype = arrayOf(
            DialogParam("本月", "month", "",true),
            DialogParam("本日", "day")
    )

    init {
        getData()
    }

    /**
     * 数据回调
     */
    fun setData(j: JSONObject) {
        Log.e("analysisToPer", j.toString())
        //业务类型  hall=> 展厅  ectel=> 网电销 secondary => 二网
        when (itype) {
            "hall" -> {
                changData(j, hallMonth, hallMonthTop)
            }
            "ectel" -> {
                changData(j, ectelMonth, ectelMonthTop)
            }
            "secondary" -> {
                changData(j, secondaryMonth, ArrayList<AnalysisParam>())
            }
        }


    }

    /**
     * 设置数据
     */
    private fun changData(j: JSONObject, Month: ArrayList<AnalysisParam>, top: ArrayList<AnalysisParam>) {
        try {
            var rate = j.optJSONObject("rate")
            var data = j.optJSONObject("data")
            var temlist = ArrayList<AnalysisParam>()
            var temlistTop = ArrayList<AnalysisParam>()
            for (i in Month) {
                var sum = data.optJSONObject(i.key).optString("sum")
                var item = AnalysisParam(i.name, i.key, i.danwei, sum)
                temlist.add(item)
            }
            //本月
            if (dtype == "month") {
                for (i in top) {
                    var sum = rate.optJSONObject(i.key).optString("sum")
                    var item = AnalysisParam(i.name, i.key, i.danwei, sum)
                    temlistTop.add(item)
                }
            }

            showlistData.postValue(temlist)
            showTopData.set(temlistTop)
        } catch (e: Exception) {
            Log.e("Exception",e.toString())

        }

    }


    /**
     * 点击业务类型
     */
    fun onClickInfotype(view: View) {
        var content = view.context

        if (content is AppCompatActivity) {

            MyToast("aaaa")
            //深拷贝
            var list = ArrayList<DialogParam>()
            for (it in infotype) {
                var item = DialogParam(it.name, it.id)
                item.IsSelect = it.IsSelect
                item.ikey = it.ikey
                list.add(item)
            }
            val newFragment = MyDialog.newInstance("业务渠道", list)
            newFragment.huidia = this::myPopSetData
            newFragment.show(content.supportFragmentManager, "missiles")
        }
    }

    /**
     * 点击确定业务渠道回调
     */
    fun myPopSetData(item: DialogParam?) {
        //设置回调参数
        item?.apply {
            for (i in infotype) {
                i.IsSelect = i.id == item.id
            }
        }

        getData()
    }


    /**
     * 请求数据
     */
    fun getData() {
        dtype = ""
        itype = ""
        for (i in datetype) {
            if (i.IsSelect) {
                dtype = i.id
                datetypename.set(i.name)
            }
        }
        for (i in infotype) {
            if (i.IsSelect) {
                itype = i.id
                infotypename.postValue(i.name)
            }
        }
        homeAnalysisFragmentRepository.getData(dtype, itype, this::setData)
    }

    /**
     * 点击时间类型  day 按天展开  month  按月展开
     */
    fun onClickDatetype(view: View) {
        var content = view.context

        if (content is AppCompatActivity) {
            //深拷贝
            var list = ArrayList<DialogParam>()
            for (it in datetype) {
                var item = DialogParam(it.name, it.id)
                item.IsSelect = it.IsSelect
                item.ikey = it.ikey
                list.add(item)
            }
            val newFragment = MyDialog.newInstance("时间", list)
            newFragment.huidia = this::mydaySetData
            newFragment.show(content.supportFragmentManager, "missiles")
        }
    }

    /**
     * 点击确定时间类型回调
     */
    fun mydaySetData(item: DialogParam?) {
        //设置回调参数
        item?.apply {
            for (i in datetype) {
                i.IsSelect = i.id == item.id
            }
        }
        getData()
    }

    fun refresh() {
        getData()
    }
}